﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models.Enums;

namespace 基于WPF的电缆采样数据分析系统.Models.Dbs.DbService
{
    /// <summary>
    /// 用户服务
    /// </summary>
    public class UserService
    {
        /// <summary>
        /// 单例模型
        /// </summary>
        public static UserService Instance = new UserService(App.GetService<TDRDbContext>());

        /// <summary>
        /// 数据库上下文
        /// </summary>
        private readonly TDRDbContext db;

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="_db"></param>
        public UserService(TDRDbContext _db)
        {
            db = _db;
        }


        /// <summary>
        /// 登录账号 返回Success则登录成功,返回Fail则登录失败
        /// </summary>
        /// <param name="LoginAccount"></param>
        /// <param name="LoginPassword"></param>
        /// <returns></returns>
        public RetResult LoginByAccountAndPassword(string LoginAccount, string LoginPassword)
        {
            try
            {
                User? user = db.Users.Where(u => u.Account == LoginAccount).FirstOrDefault();

                if (user == null)
                {
                    return RetResult.Fail;
                }

                if (string.Equals(user.Password, LoginPassword) && user.IsEnable == 1)
                {
                    return RetResult.Success;
                }

                return RetResult.Fail;
            }
            catch (Exception ex)
            {
                return RetResult.Fail;
            }
        }


        /// <summary>
        /// 根据账号和密码获取用户信息
        /// </summary>
        /// <param name="LoginAccount"></param>
        /// <param name="LoginPassword"></param>
        /// <returns></returns>
        public User GetUserByAccountAndPassword(string LoginAccount, string LoginPassword)
        {
            try
            {
                User? user = db.Users.Where(u => u.Account == LoginAccount && u.Password == LoginPassword).FirstOrDefault();

                // 用户不存在
                if (user == null)
                {
                    return null;
                }

                // 用户存在
                return user;
            }
            catch (Exception ex)
            {
                return null;
            }
        }



        /// <summary>
        /// 根据id获取用户信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public User GetUserByUid(int uid)
        {
            try
            {
                User? user = db.Users.Where(u => u.Uid == uid).FirstOrDefault();

                // 用户不存在
                if (user == null)
                {
                    return null;
                }

                // 用户存在
                return user;
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        /// 根据账号获取用户 0表示不存在 1表示存在 -1表示异常
        /// </summary>
        /// <param name="Account"></param>
        /// <returns></returns>
        public int GetUserByAccount(string Account)
        {
            try
            {
                User? user = db.Users.Where(u => u.Account == Account).FirstOrDefault();

                // 用户不存在
                if (user == null)
                {
                    return 0;
                }

                // 用户存在
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }


        /// <summary>
        /// 注册用户信息 返回Success则登录成功,返回Fail则登录失败
        /// </summary>
        /// <param name="RegisterAlias"></param>
        /// <param name="RegisterAccount"></param>
        /// <param name="RegisterPassword"></param>
        /// <param name="InvitationCode"></param>
        /// <returns></returns>
        public RetResult RegisterUser(string RegisterAlias, string RegisterAccount, string RegisterPassword)
        {
            try
            {
                User user = new User();
                user.GradeId = 2;
                user.Alias = RegisterAlias;
                user.Account = RegisterAccount;
                user.Password = RegisterPassword;
                user.IsEnable = 1;

                db.Users.Add(user);

                int rows = db.SaveChanges();

                if (rows > 0)
                {
                    return RetResult.Success;
                }

                return RetResult.Fail;
            }
            catch (Exception ex)
            {
                return RetResult.Fail;
            }
        }


        /// <summary>
        /// 获取所有普通用户信息
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<User> GetAllNormalUserList()
        {
            try
            {
                ObservableCollection<User> users = new ObservableCollection<User>(db.Users.Where(u => u.GradeId == 2));

                if (users.Count > 0)
                {
                    return users;
                }

                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        /// 根据Id删除用户
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public RetResult DeleteUserById(int uid)
        {
            try
            {

                User? user = db.Users.Where(u => u.Uid == uid).FirstOrDefault();

                // 用户不存在
                if (user == null)
                {
                    return RetResult.Fail;
                }

                // 用户存在
                db.Users.Remove(user);
                int rows = db.SaveChanges();

                // 删除成功
                if (rows > 0)
                {
                    return RetResult.Success;
                }

                return RetResult.Fail;
            }
            catch (Exception ex)
            {
                return RetResult.Fail;
            }
        }


        /// <summary>
        /// 根据用户id启用用户
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public RetResult EnableUserById(int uid)
        {
            try
            {

                User? user = db.Users.Where(u => u.Uid == uid).FirstOrDefault();

                // 用户不存在
                if (user == null)
                {
                    return RetResult.Fail;
                }

                // 用户存在
                user.IsEnable = 1;
                int rows = db.SaveChanges();

                // 启用成功
                if (rows > 0)
                {
                    return RetResult.Success;
                }

                return RetResult.Fail;
            }
            catch (Exception ex)
            {
                return RetResult.Fail;
            }
        }


        /// <summary>
        /// 根据id禁用用户
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public RetResult DisableUserById(int uid)
        {
            try
            {

                User? user = db.Users.Where(u => u.Uid == uid).FirstOrDefault();

                // 用户不存在
                if (user == null)
                {
                    return RetResult.Fail;
                }

                // 用户存在
                user.IsEnable = 0;
                int rows = db.SaveChanges();

                // 禁用成功
                if (rows > 0)
                {
                    return RetResult.Success;
                }

                return RetResult.Fail;
            }
            catch (Exception ex)
            {
                return RetResult.Fail;
            }
        }
    }
}
